<?php 
$pathArr = explode("/", $_SERVER["PHP_SELF"]);
$contextPath = $pathArr[1];
$rootPath = $_SERVER["DOCUMENT_ROOT"];
include_once $rootPath . "/" . $contextPath . "/apps/ManagerIUDS.php";
$managerIUDS = new ManagerIUDS();

$medicineData = $managerIUDS->queryData("medicine_name,price,medicine_id,amount,medicine_sub_type", "medicine", "", "2", "");
$price = $_GET["price"];
if($price == "" || $price == null){
	$price = "";
}
$data = $_GET["data"];

if($data == "" || $data == null){
	$data = "";
}
$explodeData = explode(",", $data);

?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>.: Predicate Detail :.</title>
<?php
require_once($rootPath . "/" . $contextPath . "/includes/css_inc.php");
require_once($rootPath . "/" . $contextPath . "/includes/js_inc.php");
?>
<script>
	function savePrice(){
		var data = document.getElementById("data").value;
		var medicineName = document.getElementById("medicine_name").value;
		var text = medicineName.substring(0,medicineName.length-1);
		var myText = "";
		var mySplit = text.split(",");
		for(var i=0;i<mySplit.length;i++){
			myText += mySplit[i]+"\n";
		}
		window.opener.document.maintenance_frm.medicine_price.value = document.getElementById("price").value;
		window.opener.document.maintenance_frm.medicine_data.value = data.substring(0,data.length-2);
		window.opener.document.maintenance_frm.medicine_list.value = myText;
		window.close();
	}
	function checkAmount(e){
		var myAmountArray = new Array();
		var myPriceArray = new Array();
		var myMedicineIdArray = new Array();
		var myMedicineNameArray = new Array();
		var myMedicineNameShowArray = new Array();
		var myMedicineAmounArray = new Array();
		var textUnit = new Array();
		var indexAmount = 0;
		var indexPrice = 0;
		var sum = 0;
		var idData = "";
		var nameData = "";
		var value = 0;
		var medicineNameShow = "";
		for(var i=0;i<frm1.elements.length;i++){
			if(frm1.elements[i].name=="amount" && frm1.elements[i].value != "" && frm1.elements[i].value != null){
				value = frm1.elements[i].value;
				myAmountArray[indexAmount] = value;
				indexAmount++;
			}
			if(frm1.elements[i].type=="checkbox"){
				if(frm1.elements[i].checked){
					mySplit = frm1.elements[i].value.split("|");
					myPriceArray[indexPrice] = mySplit[0];
					myMedicineIdArray[indexPrice] = mySplit[1];
					myMedicineNameArray[indexPrice] = mySplit[2];
					myMedicineAmounArray[indexPrice] = mySplit[3];
					textUnit[indexPrice] = mySplit[4];
					indexPrice++;
				}
			}
		}
		for(var x=0;x<indexPrice;x++){
			//alert("myAmountArray["+x+"] = " +myAmountArray[x] + " myPriceArray["+x+"] = " +myPriceArray[x]);
			if(myAmountArray[x] <= myMedicineAmounArray[x]){
				sum = parseInt(sum) + parseInt(parseInt(myAmountArray[x])*parseInt(myPriceArray[x])); 
				idData += myMedicineIdArray[x]+":"+myAmountArray[x]+", ";
				nameData += myMedicineNameArray[x]+":"+myAmountArray[x]+",";
				medicineNameShow += "ชื่อยา : "+ myMedicineNameArray[x]+" จำนวน : "+myAmountArray[x]+" "+textUnit[x]+",";
			}else{
				alert("คุณกรอกจำนวนยาเกินที่มี กรุณากรอกใหม่");
				document.getElementById("amount").focus;
				sum = 0;
				idData = "";
				nameData = "";
			}
		}
		document.getElementById("price").value = sum;
		document.getElementById("data").value = idData;
		document.getElementById("medicine_name").value = medicineNameShow;
		
	}
</script>
</head>
<body>
<center>
<form name="frm1">
<table border="0" width="70%">
	<thead class="ui-widget">
		<tr class="ui-widget-header">
			<th colspan="4" align="left">&nbsp;&nbsp;&nbsp;รายการยารักษา</th>
		</tr>
	</thead>
	<tbody class="ui-widget-content">
	<?php 
	$i=0;
		foreach ($medicineData as $value){
			if($value["medicine_sub_type"]=="S"){
				$text = "เม็ด : ";
				$textUnit = "เม็ด ";
			}else if($value["medicine_sub_type"]=="I"){
				$text = "เข็ม : ";
				$textUnit = "เข็ม ";
			}else{
				$text = "ขวด : ";
				$textUnit = "ขวด ";
			}
		$myArray = array();
	?>
		<tr>
			<td width="5%" align="center">
				<input type="checkbox" name="medicine_data" id="medicine_data" onclick="checkData(this)"
				<?php 
				for($i=0;$i<sizeof($explodeData);$i++){
					$buffer =  $explodeData[$i];
					$myExplodeData = explode(":", $buffer);
					if($myExplodeData[0] == $value["medicine_id"]){
						echo 'checked="checked"';
					}
				}
				?>
				value="<?=$value["price"]?>|<?=$value["medicine_id"]?>|<?=$value["medicine_name"]?>|<?=$value["amount"]?>|<?=$textUnit?>">
			</td>
			<td width="10%">
				<?=$value["medicine_name"]?>
			</td>
			<td align="left" width="10%">
				ราคา : <?=$value["price"]?>
			</td>
			<td align="left" width="15%">
				จำนวนยา /<?=$text?>
				<?php 
				for($i=0;$i<sizeof($explodeData);$i++){
					$buffer =  $explodeData[$i];
					$myExplodeData = explode(":", $buffer);
					if($myExplodeData[0] == $value["medicine_id"]){
						$amount = $myExplodeData[1];
					}
				}
				?>
				<input type="text" onblur="checkAmount(this)" name="amount" id="amount<?=$value["medicine_id"]?>" value="<?=$amount?>" size="3" onkeypress="return numbersOnly(event, false);">
			</td>
		</tr>
	<?php 
		$i++;}
	?>
		<tr>
			<td align="center" width="5%">ราคา : </td>
			<td colspan="3"><input type="text" name="price" id="price" onkeypress="return numbersOnly(event, false);" value="<?=$price?>" readonly="readonly"></td>
		</tr>
		<tr>
			<td colspan="4" align="center">
			<input  type="button" name="save_data" id="save_data" value="บันทึก" onclick="savePrice()">
			<input type="button" value="ยกเลิก"	onclick="window.close();"></td>
		</tr>
	</tbody>
</table>
<input type="hidden" name="data" id="data" value="">
<input type="hidden" name="medicine_name" id="medicine_name" value="">
</form>
</center>
</body>
</html>
